package com.whfc.emp.redis.impl;

import com.whfc.emp.redis.ZbznTokenRedisDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.concurrent.TimeUnit;

@Repository
public class ZbznTokenRedisDaoImpl implements ZbznTokenRedisDao {

    private static final String zbznTokenKey = "zbzn-token::%s";

    /**
     * 默认过期时间4小时
     */
    public static final long TIMEOUT = 4;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        String redisKey = String.format(zbznTokenKey, key);
        redisTemplate.opsForValue().set(redisKey, value, TIMEOUT, TimeUnit.HOURS);
    }

    @Override
    public String get(String key) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        String redisKey = String.format(zbznTokenKey, key);
        return redisTemplate.opsForValue().get(redisKey);
    }
}
